package gov.nhso.prov.acc.apps.acc67;

import java.sql.Connection;
import java.sql.SQLException;

import gov.nhso.prov.acc.beans.Acc67UserBean;
import gov.nhso.prov.acc.core.DBConnection;
import gov.nhso.prov.acc.utils.UserInfo;
import gov.nhso.prov.acc.utils.Utility;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.isud.core.ISUD;
import com.isud.core.ISUDFactory;
import com.isud.paging.PageBean;

public class ACC670101 extends Utility {
	public String url = "/apps/acc67/ACC670101A.jsp";
	
	public ACC670101(HttpServletRequest request, HttpServletResponse response) {
		this.message(request).clearMsg();
		String job = request.getParameter("job");
		if (job.equals("init")) {
			this.clearLastRequest(request, "lastSearch");
			this.init(request);
		} else if (job.equals("add")) {
			this.add(request);
		} else if (job.equals("search")) {
			this.setLastRequest(request, "lastSearch");
			this.search(request);
		} else if (job.equals("save")) {
			this.save(request);
		} else if (job.equals("view")) {
			this.view(request);
		} else if (job.equals("info")) {
			this.info(request);
		} else if (job.equals("update")) {
			this.update(request);
		} else if (job.equals("updateInfo")) {
			this.updateInfo(request);
		} else if (job.equals("reset")) {
			this.reset(request);
		} else if (job.equals("delete")) {
			this.delete(request);
		} else if (job.equals("back")) {
			if (this.getLastRequest(request, "lastSearch")) {
				this.search(request);
			} else {
				this.init(request);
			}
		}
	}
	
	private String init(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		this.initPage(request);

		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return this.url;
	}
	
	private String search(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			PageBean pageBean = new PageBean();
			Acc67UserBean userBean = new Acc67UserBean();
			Acc67UserBean user = new Acc67UserBean();
			String condition = "";
			this.beanUtil().copyRequest2Bean(request, pageBean);
			this.beanUtil().copyRequest2Bean(request, userBean);
			this.beanUtil().copyRequest2Bean(request, user);
			userBean.setUserId("%" + userBean.getUserId() + "%");
			userBean.setUserName("%" + userBean.getUserName() + "%");
			if (this.user(request).getAreaLevel().equals("2")){
				condition = " AND AREA_CODE IN (SELECT PROVINCE_ID FROM PROVINCE WHERE NHSO_ZONE = '" + this.user(request).getAreaCode()+ "' ) AND ROLE_ID IN ("+this.rrc+")";
			}
			pageBean = dao.selectPage(userBean, condition, pageBean);
			if (pageBean.getPageData() == null) {
				this.message(request).setMsgCode("I001");
			}
			request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			request.setAttribute("pageBean", pageBean);
			request.setAttribute("user", user);
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String add(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			if (this.user(request).getAreaLevel().equals("2")){
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvinceByZone(this.user(request).getAreaCode()));
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole(this.rrc));
			} else {
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			}
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			request.setAttribute("job", "save");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String save(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			Acc67UserBean userSet = new Acc67UserBean();
			String userId = "";
			String citizenId = "";
			String errorMessage = "";
			this.beanUtil().copyRequest2Bean(request, user);
			this.beanUtil().copyRequest2Bean(request, userSet);
			userId = dao.findDataSql("SELECT COUNT(USER_ID) AS COUNT_USER_ID FROM "+this.schema+".ACC67_USER WHERE USER_ID='"+user.getUserId()+"' ").get("COUNT_USER_ID").toString();
			citizenId = dao.findDataSql("SELECT COUNT(CITIZEN_ID) AS COUNT_CITIZEN_ID FROM "+this.schema+".ACC67_USER WHERE CITIZEN_ID='"+user.getCitizenId()+"' ").get("COUNT_CITIZEN_ID").toString();
			if (!userId.equals("0")){
				errorMessage += "รหัสผู้ใช้งานนี้มีในระบบแล้ว";
			}
			if (!citizenId.equals("0")) {
				if (!userId.equals("0")){
					errorMessage += "และ";
				}
				errorMessage += "เลขประชาชนนี้มีในระบบแล้ว";
			}
			if(userId.equals("0") && citizenId.equals("0")){
				userSet.setUserId(user.getUserId());
				userSet.setPasswd(dao.encrypt(user.getPasswd()));
				userSet.setUserName(user.getUserName());
				userSet.setEmail(user.getEmail());
				userSet.setAreaCode(user.getAreaCode());
				userSet.setActive("I");
				if (this.dp.equals("N")) { 
					userSet.setCreatedBy("sysencrypt");
				} else {
					userSet.setCreatedBy(userInfo.getUserId());
				}
				userSet.setUpdatedBy(userInfo.getUserId());                                                                                                                                                                                                                                                                
				dao.insert(userSet);
				user.setActive("I");
				this.message(request).setMsgCode("I002");
				request.setAttribute("job", "update");
			}else {
				this.message(request).setMsg("E", errorMessage);
				request.setAttribute("job", "save");
			}
			

			if (this.user(request).getAreaLevel().equals("2")){
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvinceByZone(this.user(request).getAreaCode()));
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole(this.rrc));
			} else {
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			}
			request.setAttribute("user", user);
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			request.setAttribute("userStatus", this.properties().userStatus());
			conn.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String update(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			Acc67UserBean userSet = new Acc67UserBean();
			String oldPasswd = this.getAttr(request, "oldPasswd");
			this.beanUtil().copyRequest2Bean(request, user);
			this.beanUtil().copyRequest2Bean(request, userSet);
			userSet.setUserId(user.getUserId());
			userSet.setPasswd(dao.encrypt(user.getPasswd()));
			userSet.setUserName(user.getUserName());
			userSet.setEmail(user.getEmail());
			userSet.setAreaCode(user.getAreaCode());
			if (!user.getPasswd().equals(oldPasswd)){
				userSet.setActive("I");
				user.setActive("I");
			}
			userSet.setUpdatedBy(userInfo.getUserId());
			userSet.setUpdatedDate(this.sysDate);
			dao.update(userSet);
			request.setAttribute("user", user);

			if (this.user(request).getAreaLevel().equals("2")){
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvinceByZone(this.user(request).getAreaCode()));
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole(this.rrc));
			} else {
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			}
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			request.setAttribute("job", "update");
			request.setAttribute("userStatus", this.properties().userStatus());
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String updateInfo(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			Acc67UserBean userSet = new Acc67UserBean();
			this.beanUtil().copyRequest2Bean(request, user);
			this.beanUtil().copyRequest2Bean(request, userSet);
			userSet.setUserId(user.getUserId());
			userSet.setPasswd(dao.encrypt(user.getPasswd()));
			userSet.setUserName(user.getUserName());
			userSet.setEmail(user.getEmail());
			userSet.setAreaCode(user.getAreaCode());
			userSet.setUpdatedBy(userInfo.getUserId());
			userSet.setUpdatedDate(this.sysDate);
			dao.update(userSet);
			request.setAttribute("user", user);
			if (this.user(request).getAreaLevel().equals("2")){
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvinceByZone(this.user(request).getAreaCode()));
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole(this.rrc));
			} else {
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			}
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101C.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String reset(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			Acc67UserBean userSet = new Acc67UserBean();
			this.beanUtil().copyRequest2Bean(request, user);
			userSet.setUserId(user.getUserId());
			userSet.setPasswd(user.getUserId());
			userSet.setUpdatedBy(userInfo.getUserId());
			userSet.setUpdatedDate(this.sysDate);
			dao.update(userSet);
			request.setAttribute("user", user);
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
			request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			request.setAttribute("job", "update");
			this.message(request).setMsgCode("I002");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String view(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			this.beanUtil().copyRequest2Bean(request, user);
			user.setUserId(user.getUserId());
			user = (Acc67UserBean) dao.select(user);
			user.setPasswd(dao.decrypt(user.getPasswd()));
			request.setAttribute("user", user);
			
			if (this.user(request).getAreaLevel().equals("2")){
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvinceByZone(this.user(request).getAreaCode()));
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole(this.rrc));
			} else {
				request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
				request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			}
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			request.setAttribute("job", "update");
			request.setAttribute("userStatus", this.properties().userStatus());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101B.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String info(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			this.beanUtil().copyRequest2Bean(request, user);
			user.setUserId(user.getUserId());
			user = (Acc67UserBean) dao.select(user);
			user.setPasswd(dao.decrypt(user.getPasswd()));
			request.setAttribute("user", user);
			request.setAttribute("lsNhsoZone", this.dataUtil(dao).lsNhsoZone());
			request.setAttribute("lsProvince", this.dataUtil(dao).lsProvince());
			request.setAttribute("lsRole", this.dataUtil(dao).lsRole());
			request.setAttribute("areaLevel", this.properties().areaLevel());
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.url = "/apps/acc67/ACC670101C.jsp";
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
	
	private String delete(HttpServletRequest request) {
		Connection conn = null;
		ISUD dao = null;
		try {
			UserInfo userInfo = this.user(request);
			conn = DBConnection.getConnection();
			dao = ISUDFactory.create(conn);
			Acc67UserBean user = new Acc67UserBean();
			this.beanUtil().copyRequest2Bean(request, user);
			user.setUserId(user.getUserId());
			user.setActive("N");
			user.setUpdatedBy(userInfo.getUserId());
			user.setUpdatedDate(this.sysDate);
			dao.update(user);
			this.message(request).setMsgCode("I003");
			conn.commit();
		} catch (Exception e) {
			try {
				if (conn != null) {
					conn.rollback();
				}
			} catch (SQLException eSQL) {
				eSQL.printStackTrace();
			}
			this.message(request).setMsg("E", e.toString() + ":" + dao.getMessage(), e.getStackTrace());
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
				dao = null;
				this.getLastRequest(request, "lastSearch");
				this.url = this.search(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return this.url;
	}
}
